﻿using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using BaseThing.Engine;

namespace BaseThing.Game
{

    class Link : GameObject
    {
        enum DIR
        {
            LEFT = 0,
            RIGHT,
            UP,
            DOWN
        };

        Sprite linkSpr = null;
        //Row, Col
        //Start, End
        Point upWalkAnimation = new Point(1, 3);
        Point downWalkAnimation = new Point(141, 154);
        Point rightWalkAnimation = new Point(13, 15);
        Point leftWalkAnimation = new Point(37, 39);

        DIR lastDir = DIR.RIGHT;
        Point[] idleAnimations;

        public Link(Vector2 Position)
            : base()
        {
            linkSpr = new Sprite(@"Textures/mclinkcap4fe2", 47, 23, 8);
            linkSpr.SetMask(new Color(32, 156, 0));
            linkSpr.Position = Position;
            this.Position = Position;

            linkSpr.SetAnimation(downWalkAnimation);
            AddSprite(linkSpr);

            //Idle Animations
            idleAnimations = new Point[4];

            idleAnimations[0] = new Point(38, 38); //LeftIdle
            idleAnimations[1] = new Point(14, 14); //RightIdle
            idleAnimations[2] = new Point(2, 2);   //UpIdle
            idleAnimations[3] = new Point(26, 26); //IdleDown
        }


        public void Update(GameTime gameTime, KeyboardState keyboardState)
        {

            bool keyPressed = false;

            if (Keyboard.GetState().IsKeyDown(Keys.I))
            {
                linkSpr.SetAnimation(upWalkAnimation);
                Move(new Vector2(0, -1));
                lastDir = DIR.UP;
                keyPressed = true;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.K))
            {
                linkSpr.SetAnimation(downWalkAnimation);
                Move(new Vector2(0, 1));
                lastDir = DIR.DOWN;
                keyPressed = true;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.L))
            {
                linkSpr.SetAnimation(rightWalkAnimation);
                Move(new Vector2(1, 0));
                lastDir = DIR.RIGHT;
                keyPressed = true;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.J))
            {
                linkSpr.SetAnimation(leftWalkAnimation);
                Move(new Vector2(-1, 0));
                lastDir = DIR.LEFT;
                keyPressed = true;
            }

            if (!keyPressed)
            {
                linkSpr.SetAnimation(idleAnimations[(int)lastDir]);
            }


            base.Update(gameTime);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {

            base.Draw(spriteBatch);
        }
    }
}
